package com.his.controller;

import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.Schedule;
import com.his.service.ScheduleService;
import com.his.utils.MyUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 排班表控制层
 *
 * @author makejava
 * @since 2020-10-26 10:04:38
 */
@RestController
@RequestMapping("schedule")
public class ScheduleController {
    /**
     * 排班service对象
     */
    @Resource
    private ScheduleService scheduleService;

    /**
     * 添加日志
     *
     * @param schedule 日志对象
     * @return 返回添加状态
     */
    @RequireRoles(roles = {"hr","admin"}, index = 0)
    @PostMapping("add")
    public Rsp add(@RequestHeader(Const.TOKEN) String token, @Validated @RequestBody Schedule schedule) {
        return scheduleService.insert(schedule) ;
    }

    /**
     * 查询排班
     *
     * @param pageParam 前端传过来的分页参数
     * @return 返回查询结果
     */
    @RequireRoles(roles = {"hr","admin"}, index = 0)
    @PostMapping("getAll")
    public Rsp getAll(@RequestHeader(Const.TOKEN) String token,  @RequestBody PageParam<Schedule> pageParam) {
        return scheduleService.queryAll(pageParam);
    }

    @RequireRoles(roles = {"hr","admin"}, index = 0)
    @PostMapping("update")
    public Rsp update(@RequestHeader(Const.TOKEN) String token, @RequestBody Schedule schedule) {
        return scheduleService.update(schedule);
    }

    @GetMapping("getSchedule")
    public Rsp getSchedule( @RequestHeader(Const.TOKEN) String token) throws Exception {
        return Rsp.success(scheduleService.getSchedule(MyUtil.getUserByToken(token)));
    }

    @RequireRoles(roles = {"admin"}, index = 0)
    @GetMapping("deleteById")
    public Rsp deleteById(@RequestHeader(Const.TOKEN) String token,@NotNull Integer id){
        return Rsp.success(scheduleService.deleteById(id));
    }
}